package com.wzcl.app.controller;

import com.wzcl.app.dao.model.Orders;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.OrdersDto;
import com.wzcl.app.model.dto.PayRecordDto;
import com.wzcl.app.model.vo.OrderVo;
import com.wzcl.app.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Log4j2
@RestController
@RequestMapping("/order")
@Api(value = "订单管理Controller", tags = {"订单管理相关接口"})
public class OrderController {

    @Resource
    private OrderService orderService;

    @ApiOperation("获取订单")
    @PostMapping("/get")
    public RVO<OrderVo> getOrder(@RequestBody OrdersDto ordersDto){
        Orders order = orderService.getOrderById(ordersDto.getId());
        if(order != null) {
            return RVO.success(OrderVo.create(order));
        }
        return RVO.success(null);
    }

    @PostMapping("/getList")
        @ApiOperation("订单列表List")
    public RVO<List<OrderVo>> getOrderList(@RequestBody OrdersDto orderDto){
        List<Orders> list = orderService.getOrderList(orderDto);
        if(list != null && list.size() > 0){
            return RVO.success(list.stream().map(OrderVo::create).collect(Collectors.toList()));
        }
        return RVO.success(null);
    }

    @PostMapping("/getPage")
    @ApiOperation("订单列表Page")
    public RVO<List<OrderVo>> getOrderPage(@RequestBody OrdersDto orderDto) throws NoSuchMethodException {
        MyPage<Orders> page = orderService.getOrderPage(orderDto);
        if(page != null && page.getContent().size() > 0){
            return RVO.success(page.getContent().stream().map(OrderVo::create).collect(Collectors.toList()),page);
        }
        return RVO.success(null);
    }

    @GetMapping("/getNotPayOrder")
    @ApiOperation("获取未支付订单")
    public RVO findAllNotPayByPayState(){
        return orderService.findAllNotPayByPayState();
    }

    @DeleteMapping("/deleteByIds")
    @ApiOperation("根据订单id批量删除订单")
    public RVO deleteByIds(@RequestBody List<Integer> ids){
        orderService.deleteByIds(ids);
        return RVO.success();
    }

    @PostMapping("/retrieve/date")
    @ApiOperation("根据日期查询模糊匹配订单")
    public RVO retrieveByDate(@RequestBody OrdersDto ordersDto){
        return RVO.success(orderService.retrieveByDate(ordersDto));
    }
}